package com.htx.zqs.blesmartmask.adapter;

import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.htx.zqs.blesmartmask.MyApplication;
import com.htx.zqs.blesmartmask.R;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by zhao on 2018/5/12.
 */

public class RvDeviceAdapter extends RecyclerView.Adapter {

    private  Activity activity;
    private int ITEM_HEADER = 0;
    private ArrayList devices;
    private OnItemClickListener mOnItemClickListener;
    public RvDeviceAdapter(Activity activity) {
        this.activity = activity ;
    }

    public void setData(ArrayList devices) {
        this.devices = devices;
        notifyDataSetChanged();
    }

    public ArrayList getData() {
        return devices;
    }

    /**
     * @param parent----------注意填充方法 会导致item显示不正确
     * @param viewType
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_HEADER) {   //  条目的类型
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycleviewitem_device, parent, false);
            // View.inflate(activity, R.layout.recycleviewitem_device, null);
            DeviceViewHolder viewHolder = new DeviceViewHolder(view);
            return viewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

        BluetoothDevice bluetoothDevice = (BluetoothDevice) devices.get(position);
        String string = "";
        if (bluetoothDevice == null || bluetoothDevice.getName() == null) {
            string = activity.getString(R.string.unknown);
        } else {
            string = bluetoothDevice.getName();
            if (bluetoothDevice.getName().startsWith("Metis Genie Mask")) {
                string = activity.getString(R.string.app_name);
            }
        }
        if (MyApplication.isConnected && MyApplication.currentDevicePosition == position) {
            ((DeviceViewHolder) holder).iconConnected.setVisibility(View.VISIBLE);
        } else {
            ((DeviceViewHolder) holder).iconConnected.setVisibility(View.GONE);
        }
        ((DeviceViewHolder) holder).deviceName.setText(string);
        ((DeviceViewHolder) holder).deviceAddress.setText(bluetoothDevice.getAddress().substring(9));
        ((DeviceViewHolder) holder).setPosition(position);
        if (position % 2 != 0) {
            ((DeviceViewHolder) holder).item.setBackgroundColor(activity.getResources().getColor(R.color.white));
            ((DeviceViewHolder) holder).iconConnected.setImageDrawable(activity.getResources().getDrawable(R.drawable.icon_connecting));
        } else {
            ((DeviceViewHolder) holder).item.setBackgroundColor(activity.getResources().getColor(R.color.smartmask));
            ((DeviceViewHolder) holder).iconConnected.setImageDrawable(activity.getResources().getDrawable(R.drawable.icon_connect_red));
        }

        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onClick(position);
                    if (MyApplication.currentDevicePosition != position) {
                        MyApplication.currentDevicePosition = position;
                        if (MyApplication.isConnected) {
                            ((DeviceViewHolder) holder).iconConnected.setVisibility(View.VISIBLE);
                        } else {
                            ((DeviceViewHolder) holder).iconConnected.setVisibility(View.GONE);
                        }
                    }
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemClickListener.onLongClick(position);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (devices != null) {
            return devices.size();
        }
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        return ITEM_HEADER;
    }

    public interface OnItemClickListener {
        void onClick(int position);

        void onLongClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }


    class DeviceViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.device_name)
        TextView deviceName;
        @InjectView(R.id.device_address)
        TextView deviceAddress;
        @InjectView(R.id.icon_connected)
        ImageView iconConnected;
        @InjectView(R.id.item)
        LinearLayout item;
        private int position;

        DeviceViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }

        public void setPosition(int position) {
            this.position = position;
        }
    }
}
